<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <div slot="detail">
      <a-form-model ref="form" :model="model" :rules="validatorRules" >
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="版本号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="versionNumber">
              <a-input v-model="model.versionNumber" placeholder="请输入版本号" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="版本标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="title">
              <a-input v-model="model.title" placeholder="请输入版本标题" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="版本类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag placeholder="请选择版本类型" v-model="model.type" dictCode="app_version_type"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="安装包路径" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="url">
              <a-input v-model="model.url" placeholder="请输入安装包路径" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="是否强制更新" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isForcedUpdate">
              <j-dict-select-tag placeholder="请选择" v-model="model.isForcedUpdate" dictCode="mk_whether"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="是否使用" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isUsing">
              <j-dict-select-tag placeholder="请选择" v-model="model.isUsing" dictCode="mk_whether"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" placeholder="请输入备注" ></a-textarea>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="分站id集合" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="siteIds">
              <a-textarea v-model="model.siteIds" placeholder="请输入分站id集合，逗号分隔" ></a-textarea>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="用户id集合" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="userIds">
              <a-textarea v-model="model.userIds" placeholder="请输入用户id集合，逗号分隔" ></a-textarea>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
      </div>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'

  export default {
    name: 'AppVersionForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          versionNumber: [
            { required: true, message: '请输入版本号'},
          ],
          title: [
            { required: true, message: '请输入协议标题'},
          ],
          type: [{
            required: true, message: '请选择版本类型'
          }],
          url: [{
            required: true, message: '请输入安装包路径'
          }],
        },
        url: {
          add: "/appVersion",
          edit: "/appVersion",
          byId: "/appVersion/byId",
          contains: "/appVersion/contains"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.visible = true;
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>